<?php


namespace app\api\controller\v1;


use app\api\model\BikeModel;
use app\api\model\ReparkOrderModel;
use app\common\controller\UserBaseController;
use app\common\library\DLock;
use think\Db;

class RporderController extends UserBaseController
{

    function submitAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
            'user_coordinate' => 'floatarray',
            'bluetooth' => ['optint', 0],
            'unlock' => ['optint', 1],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['bike_id'], 60);
        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['bike_id']);
        if (empty($bike_info)) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败!");
        }

        if ($bike_info['city_id'] != $this->city_id) {
            $this->throw_myexception(X_ERROR, "查询车辆信息失败!!");
        }

        if ($bike_info['repark_index'] == 0) {
//            throw new MyException(X_ERROR, "不能挪动非差车!");
        }

        $ret = (new ReparkOrderModel())->submit($this->city_id, $bike_info, $this->argv['bluetooth'], $this->argv['unlock']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function finishAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'park_id' => 'optstring',
            'city_id' => 'optstring',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);

        $city_id = $this->city_id;

        if (empty($city_id)){
            if (!empty($this->argv['order_id'])){
                $city_id = substr($this->argv['order_id'],0,6);
            }
        }

        $ret = (new ReparkOrderModel())->finish($city_id, $this->argv['order_id'], $this->argv['park_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_state' => 'optint',
            'is_order_finished' => 'optint',
            'user_id' => 'optstring',
            'start_park_state' => 'optint',
            'is_flush' => 'optint',
            'start_time' => 'optdatetime',
            'end_time' => 'optdatetime',
            'pno' => 'int',
            'psize' => 'int',
            'sk' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        if (\ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_OPS) {
            //$this->argv['user_id'] = ApiOper::instance()->userinfo()['id'];
        }

        $list = (new ReparkOrderModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();

        return $this->ret_resp();
    }

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ReparkOrderModel())->query_info($this->city_id, $this->argv['order_id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function trackAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ReparkOrderModel())->query_track($this->city_id, $this->argv['order_id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function user_monthly_statAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'user_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ReparkOrderModel())->stats_month_query($this->city_id, $this->argv['user_id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    //挪车距离查看
    function query_distanceAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'user_coordinate' => ['optfloatarray', [0, 0]],
            'park_id' => 'optstring',
            'city_id' => 'optstring',
            'bluetooth' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $lock = new DLock($this->argv['order_id'], 60);

        $city_id = $this->city_id;
        if (empty($city_id)){
            if (!empty($this->argv['order_id'])){
                $city_id = substr($this->argv['order_id'],0,6);
            }
        }

        $ret = (new ReparkOrderModel())->query_distance($city_id, $this->argv['order_id'], $this->argv['park_id'], $this->argv['bluetooth']);
        if ($ret[0] != 0) {
            $this->throw_myexception($ret[0], $ret[1]);
        }

        $this->resp['msg'] = $ret[1];
        $this->resp['info'] = $ret[2];

        return $this->ret_resp();
    }


    function modify_gradeAction(){
        $this->paramlist = array(
            'token' => 'string',
            'order_id' => 'string',
            'grade' =>'int',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ReparkOrderModel())->query_info($this->city_id, $this->argv['order_id']);


        if (\ApiOper::instance()->userinfo()['user_type'] == OPER_TYPE_OPS) {

        }

        $gradeinfo = $info['grade_info'];
        $name = \ApiOper::instance()->userinfo()['name'];
        if (!empty($gradeinfo['remark'])){
            $gradeinfo['remark'] = $gradeinfo['remark'].'('.$name.'修改)';
        }
        else{
            $gradeinfo['remark'] = '('.$name.'修改)';
        }


        $order_info =[
            'grade' => $this->argv['grade'],
            'grade_info' =>$gradeinfo,
        ];

        $where = [
            'id' => $this->argv['order_id'],
        ];

        Db::table('tb_bike_repark_order')->where($where)->update($order_info);

        return $this->ret_resp();
        //hisql::update('tb_bike_repark_order', $order_info,$where);
    }
}
